﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BDHomePage.BLL
{
    public class Link : ICloneable
    {
        private int _id;
        public int Id
        {
            get { return this._id; }
        }

        private string _url;
        public string Url
        {
            get { return this._url; }
            set { this._url = value; }
        }

        private string _description;
        public string Description
        {
            get { return this._description; }
            set { this._description = value; }
        }

        private double _currentRating;
        public double CurrentRating
        {
            get { return this._currentRating; }
        }

        private int _totalRatingVote;
        public int TotalRatingVote
        {
            get { return this._totalRatingVote; }
        }

        private int _totalVisits;
        public int TotalVisits
        {
            get { return this._totalVisits; }
        }

        private LinkCategory _category;
        public LinkCategory Category
        {
            get { return this._category; }
            set { this._category = value; }
        }

        private bool _selected;
        public bool Selected
        {
            get { return this._selected; }
            set { this._selected = value; }
        }

        private bool _flagged;
        public bool Flagged
        {
            get { return this._flagged; }
        }

        public Link()
        {
            this._id = 0;
            this._category = null;
            this._currentRating = 0;
            this._description = string.Empty;
            this._flagged = false;
            this._selected = false;
            this._totalRatingVote = 0;
            this._totalVisits = 0;
            this._url = string.Empty;
        }

        public Link(int id, string url, LinkCategory category, double currentRating, string description, 
            bool flagged, bool selected, int totalRatingVote, int totalVisits )
        {
            this._id = id;
            this._category = category;
            this._currentRating = currentRating;
            this._description = description;
            this._flagged = flagged;
            this._selected = selected;
            this._totalRatingVote = totalRatingVote;
            this._totalVisits = totalVisits;
        }

        public void AddLink()
        {
            // TODO: DAL.CreateLink(Url, 
        }

        public void RemoveLink()
        {

        }

        public void UpdateLink()
        {

        }

        private void CalculateRating(double newRating)
        {
            this._currentRating = ((CurrentRating * TotalRatingVote) + newRating)/(TotalRatingVote +1);
        }

        public void CastRatingVote(double rating)
        {
            CalculateRating(rating);
            this._totalRatingVote++;
        }

        public void AddLinkToBasket(LinkBasket userBasket)
        {
            userBasket.AddLinkToBasket(this);
        }

        public void VisitLink()
        {
            this._totalVisits++;
            // Code to update db
        }

        public void AddToDelicious(string deliciousUserName, string deliciousPassword)
        {
            Delicious.Connection.Username = deliciousUserName;
            Delicious.Connection.Password = deliciousPassword;

            string url = Url;
            string description = Description;
            string extended = "";
            string tags = Category.CategoryName;
            string date = DateTime.Now.ToShortDateString();
            bool replace = false;
            bool shared = false;

            Delicious.Post.Add(url, description, extended, tags, date, replace, shared);
        }

        public void FlagLink()
        {
            if (!Flagged)
            {
                this._flagged = true;
                // DB code to update flage
            }
        }

        public void UnFlagLink()
        {
            if (Flagged)
            {
                this._flagged = false;
                // DB code to update flage
            }
        }

        #region ICloneable Members

        public object Clone()
        {
            Link copy = new Link(Id, Url, Category, CurrentRating, 
                Description, Flagged, Selected, TotalRatingVote, TotalVisits);
            return copy;
        }

        #endregion
    }
}
